<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Search Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa; /* 浅灰色背景 */
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .search-container {
            background-color: #fff; /* 白色背景 */
            padding: 40px;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
            border-radius: 10px; /* 圆角 */
            text-align: center;
            position: relative;
        }

        .search-box {
            width: 400px; /* 搜索框宽度 */
            height: 50px; /* 搜索框高度 */
            font-size: 18px; /* 字体大小 */
            padding: 10px;
            border: 2px solid #ddd; /* 边框颜色 */
            border-radius: 5px;
            outline: none;
        }

        .search-box:focus {
            border-color: #007bff; /* 聚焦时边框颜色 */
        }

        .search-btn {
            height: 50px;
            padding: 0 20px;
            font-size: 18px;
            color: white;
            background-color: #007bff; /* 按钮颜色 */
            border: none;
            border-radius: 5px;
            margin-left: 10px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .search-btn:hover {
            background-color: #0056b3; /* 按钮悬停颜色 */
        }

        /* 下拉提示框样式 */
        .suggestions-box {
            position: absolute;
            width: 400px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-top: none;
            max-height: 200px;
            overflow-y: auto;
            z-index: 10;
        }

        .suggestion-item {
            padding: 10px;
            cursor: pointer;
            border-bottom: 1px solid #ddd;
        }

        .suggestion-item:hover {
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>

    <div class="search-container">
        <input type="text" id="searchInput" class="search-box" placeholder="Search..." oninput="fetchSuggestions()">
        <button class="search-btn" onclick="search()">Search</button>

        <div id="suggestionsBox" class="suggestions-box" style="display: none;"></div> <!-- 下拉提示框 -->
    </div>

    <script>
        function fetchSuggestions() {
            var query = document.getElementById('searchInput').value;

            if (query) {
                fetch('/search', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ query: query }) // 将搜索内容作为 JSON 发送
                })
                .then(response => response.json())
                .then(data => {
                    showSuggestions(data); // 显示下拉提示框
                })
                .catch(error => {
                    console.error('Error:', error);
                });
            } else {
                hideSuggestions(); // 如果搜索框为空，则隐藏下拉提示框
            }
        }

        function showSuggestions(suggestions) {
            var suggestionsBox = document.getElementById('suggestionsBox');
            suggestionsBox.innerHTML = ''; // 清空之前的提示内容

            suggestions.forEach(function(suggestion) {
                var suggestionItem = document.createElement('div');
                suggestionItem.classList.add('suggestion-item');
                suggestionItem.textContent = suggestion;
                suggestionItem.onclick = function() {
                    document.getElementById('searchInput').value = suggestion; // 点击提示项后将其填充到搜索框
                    hideSuggestions(); // 点击后隐藏提示框
                };
                suggestionsBox.appendChild(suggestionItem);
            });

            suggestionsBox.style.display = 'block'; // 显示提示框
        }

        function hideSuggestions() {
            document.getElementById('suggestionsBox').style.display = 'none'; // 隐藏提示框
        }

        function search() {
            var query = document.getElementById('searchInput').value;

            if (query) {
                fetch('/search', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ query: query }) // 将搜索内容作为 JSON 发送
                })
                .then(response => response.text())
                .then(data => {
                    alert(data); // 显示服务器返回的响应
                })
                .catch(error => {
                    console.error('Error:', error);
                });
            } else {
                alert('Please enter a search query!');
            }
        }
    </script>

</body>
</html>
